<?php
namespace hangpu8\hpcms\Utils\Builder;

class GroupForm
{
    // Tab规则数据
    private $data = [];

    /**
     * 构造函数
     *
     * @param string $url
     */
    public function __construct($url = '')
    {
        $this->data = [
            'config'            => [
                'api'        => '',
                'method'        => 'POST',
            ],
            'formRule'          => [],
        ];
        if (empty($url))
        {
            $request = request();
            $server = $request->server();
            $query = $request->request()['s'];
            $HTTP_HOST = $server['HTTP_HOST'];
            $REQUEST_SCHEME = $server['REQUEST_SCHEME'];
            $url = "{$REQUEST_SCHEME}://{$HTTP_HOST}{$query}";
        }
        $this->data['config']['api'] = $url;
    }

    /**
     * 添加Tab表单
     *
     * @param string $field
     * @param string $title
     * @param object $callback
     * @return $this
     */
    public function addGroup(string $field,string $title,object $callback)
    {
        $rule = [
            'name'              => $field,
            'title'             => $title,
            'formRule'          => $callback($this),
        ];
        array_push($this->data['formRule'],$rule);
        return $this;
    }

    /**
     * 获取Tab子form规则
     *
     * @return array
     */
    public function getGroupRule()
    {
        return $this->builder->formRule();
    }


    /**
     * 设置请求方式
     *
     * @param [type] $method
     * @return $this
     */
    public function setMethod($method = 'GET')
    {
        $this->data['config']['method'] = $method;
        return $this;
    }

    /**
     * 设置配置项
     *
     * @param [type] $name
     * @param [type] $value
     * @return $this
     */
    public function setConfig($name,$value)
    {
        $this->data['config'][$name] = $value;
        return $this;
    }

    /**
     * 快速生成表单
     *
     * @return array
     */
    public function create()
    {
        return $this->data;
    }
}